package ift605.EquationWithAgent.server;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MessageSerializer {

	public static String serializeMessage(EquationMessage aMessage){
		String result = "";
		try{
			ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(byteOut);
	        out.writeObject(aMessage);
	        out.close();
	        result = byteOut.toString();
	        byteOut.close();
	    }
		catch(IOException i){
			i.printStackTrace();
		}
		return result;
	}
	
	public static EquationMessage deserializeMessage(String aMessage){
		EquationMessage equationMessage = null;
		try {
			ByteArrayInputStream byteInput = new ByteArrayInputStream(aMessage.getBytes());
		
			ObjectInputStream input = new ObjectInputStream(byteInput);
			EquationMessage value = (EquationMessage) input.readObject();
			input.close();
			byteInput.close();
			equationMessage = new EquationMessage(value.getRequestNumber(), value.getEquation(), value.getIdentifier());
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return equationMessage;
	}
}
